package com.corpcredit.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;

import com.corpcredit.dao.CorpDao;
import com.corpcredit.model.CorpEntity;

public class CorpDaoImpl extends BaseDaoImpl implements CorpDao{
	private static final Logger logger = Logger.getLogger(CorpDaoImpl.class);

	public boolean addCorp(final CorpEntity corp) {
		boolean flag = false;
		int row = jdbcTemplate.update("insert into qy_jbqk values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,GETDATE(),?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
				new PreparedStatementSetter() {
					public void setValues(PreparedStatement ps)
							throws SQLException {
						ps.setString( 1, corp.getQyid() );
			            ps.setString( 2, corp.getQyqc() );
			            ps.setString( 3, corp.getQywz() );
			            ps.setString( 4, corp.getZcdz() );
			            ps.setString( 5, corp.getYyzzzch() );
			            ps.setString( 6, corp.getZzjgdm() );
			            ps.setString( 7, corp.getDsdjh() );
			            ps.setString( 8, corp.getGsdjh() );
			            ps.setString( 9, corp.getDkkh() );
			            ps.setString( 10, corp.getJbhkhh() );
			            ps.setString( 11, corp.getJbhzh() );
			            ps.setString( 12, corp.getYbhkhh() );
			            ps.setString( 13, corp.getYbhzh() );
			            ps.setString( 14, corp.getJydz() );
			            ps.setString( 15, corp.getYb() );
			            ps.setString( 16, corp.getDh() );
			            ps.setString( 17, corp.getCz() );
			            ps.setString( 18, corp.getZczbbz() );
			            ps.setDouble( 19, corp.getZczbje() );
			            ps.setString( 20, corp.getClrq() );
			            ps.setString( 21, corp.getFrdb() );
			            ps.setString( 22, corp.getHyfl() );
			            ps.setString( 23, corp.getYwfw() );
			            ps.setDouble( 24, corp.getJycdmj() );
			            ps.setString( 25, corp.getFcsyq() );
			            ps.setDouble( 26, corp.getCyrs() );
			            ps.setDouble( 27, corp.getZzc() );
			            ps.setDouble( 28, corp.getXsze() );
			            ps.setString( 29, corp.getZljzrq() );
			            ps.setString( 30, corp.getQylx() );
			            ps.setString( 31, corp.getQygm() );
			            ps.setString( 32, corp.getDabh() );
			            ps.setString( 33, corp.getQycym() );
			            ps.setString( 34, corp.getKhlx() );
			            ps.setString( 35, corp.getSsqy() );
			            ps.setString( 36, corp.getOptrid() );
			            ps.setString( 37, corp.getRecstate() );
			            ps.setString( 38, corp.getRemark() );
			            ps.setInt( 39, corp.getCompanyID() );
			            ps.setShort( 40, corp.getSffr() );
			            ps.setShort( 41, corp.getSfsydw() );
			            ps.setString( 42, corp.getEnglishname() );
			            ps.setString( 43, corp.getEmail() );
			            ps.setString( 44, corp.getZycpmc() );
			            ps.setString( 45, corp.getJyqx() );
			            ps.setString( 46, corp.getJjlx() );
			            ps.setString( 47, corp.getJglx() );
			            ps.setString( 48, corp.getJyzt() );
			            ps.setShort( 49, corp.getSfxj() );
			            ps.setString( 50, corp.getSfgxjsqy() );
					}
				});
		if(row == 1) flag = true;
		return flag;
	}

	public boolean delCorpByQyid(String qyid) {
		Object[] paras = {qyid};
		int result = jdbcTemplate.update("delete FROM qy_jbqk where qyid = ?", paras);
		if(result == 1) return true;
		else return false;
	}
	
	public boolean delCorpByQyqc(String qyqc) {
		Object[] paras = {qyqc};
		int result = jdbcTemplate.update("delete FROM qy_jbqk where qyqc = ?", paras);
		if(result == 1) return true;
		else return false;
	}

	public boolean editCorp( String qyid, String strSet ) {
        String strSql = "update qy_jbqk set " + strSet + " where qyid='"
        + qyid + "'";
        int result = jdbcTemplate.update(strSql);
		if(result == 1) return true;
		else return false;
	}

	public CorpEntity getCorpEntityByQyid(String qyid) {
		List result = jdbcTemplate.query("select * from qy_jbqk where qyid = ?", new Object[]{qyid}, new CorpRowMapper());
		if(result.size()==0) return null;
		return (CorpEntity)result.get(0);
	}

	public List<CorpEntity> getCorpListByFilter(String filter) {
		return jdbcTemplate.query("select * from qy_jbqk where " + filter, new CorpRowMapper());
	}

	public String getQyidByQyqc(String qyqc) {
		String[] paras = {qyqc};
		Object result = jdbcTemplate.queryForObject("select qyid from qy_jbqk where qyqc= ?", paras, String.class);
		if(result == null) return null;
		return (String)result;
	}
	

	class CorpRowMapper implements RowMapper{

		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpEntity corp = new CorpEntity();
			corp.setQyid(rs.getString(1));
			corp.setQyqc(rs.getString(2));
			corp.setQywz(rs.getString(3));
			corp.setZcdz(rs.getString(4));
			corp.setYyzzzch(rs.getString(5));
			corp.setZzjgdm(rs.getString(6));
			corp.setDsdjh(rs.getString(7));
			corp.setGsdjh(rs.getString(8));
			corp.setDkkh(rs.getString(9));
			corp.setJbhkhh(rs.getString(10));
			corp.setJbhzh(rs.getString(11));
			corp.setYbhkhh(rs.getString(12));
			corp.setYbhzh(rs.getString(13));
			corp.setJydz(rs.getString(14));
			corp.setYb(rs.getString(15));
			corp.setDh(rs.getString(16));
			corp.setCz(rs.getString(17));
			corp.setZczbbz(rs.getString(18));
			corp.setZczbje(rs.getDouble(19));
			corp.setClrq(rs.getString(20));
			corp.setFrdb(rs.getString(21));
			corp.setHyfl(rs.getString(22));
			corp.setYwfw(rs.getString(23));
			corp.setJycdmj(rs.getDouble(24));
			corp.setFcsyq(rs.getString(25));
			corp.setCyrs(rs.getDouble(26));
			corp.setZzc(rs.getDouble(27));
			corp.setXsze(rs.getDouble(28));
			corp.setZljzrq(rs.getString(29));
			corp.setQylx(rs.getString(30));
			corp.setQygm(rs.getString(31));
			corp.setDabh(rs.getString(32));
			corp.setQycym(rs.getString(33));
			corp.setKhlx(rs.getString(34));
			corp.setSsqy(rs.getString(35));
			corp.setOptrid(rs.getString(36));
			corp.setOptdate(rs.getDate(37));
			corp.setRecstate(rs.getString(38));
			corp.setRemark(rs.getString(39));
			corp.setCompanyID(rs.getInt(40));
			corp.setSffr(rs.getShort(41));
			corp.setSfsydw(rs.getShort(42));
			corp.setEnglishname(rs.getString(43));
			corp.setEmail(rs.getString(44));
			corp.setZycpmc(rs.getString(45));
			corp.setJyqx(rs.getString(46));
			corp.setJjlx(rs.getString(47));
			corp.setJglx(rs.getString(48));
			corp.setJyzt(rs.getString(49));
			corp.setSfxj(rs.getShort(50));
			corp.setSfgxjsqy(rs.getString(51));
			return corp;
		}
		
	}

}
